#ifndef NSSHARE_UTILS_SCOPE_HANDLE_H_
#define NSSHARE_UTILS_SCOPE_HANDLE_H_

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

namespace nsshare
{
namespace utils
{

class ScopeHandle
{
public:
    ScopeHandle()
        : handle_(0) { }

    ScopeHandle(HANDLE handle)
        : handle_(handle){ }

    ~ScopeHandle() 
    { 
        if(handle_ != 0 && handle_ != INVALID_HANDLE_VALUE)
        {
            ::CloseHandle(handle_);
            handle_ = 0;
        }
    }

    ScopeHandle & operator=(HANDLE handle)
    {
        handle_ = handle;
    }

    HANDLE * operator&()
    {
        return &handle_;
    }

    bool operator==(HANDLE handle)
    {
        return handle_ == handle;
    }

    operator HANDLE() const
    {
        return handle_;
    }

    bool IsValid() const
    {
        if(handle_ == NULL)
            return false;
        if(handle_ == INVALID_HANDLE_VALUE)
            return false;
        return true;
    }
private:
    HANDLE handle_;
};

}
}

#endif